#define _CRT_SECURE_NO_WARNINGS 1
class Solution {
public:
    int m, n;

    vector<vector<char>> updateBoard(vector<vector<char>>& board, vector<int>& click) {
        m = board.size(), n = board[0].size();
        int x = click[0], y = click[1];
        dfs(board, x, y);
        return board;
    }
    void dfs(vector<vector<char>>& board, int i, int j)
    {
        if (board[i][j] == 'M')
        {
            board[i][j] = 'X';
            return;
        }
        int sum = 0;
        for (int x = i - 1; x <= i + 1; x++)
        {
            for (int y = j - 1; y <= j + 1; y++)
            {
                if (x >= 0 && x < m && y >= 0 && y < n && board[x][y] == 'M')
                    sum++;
            }
        }
        if (sum)
        {
            board[i][j] = '0' + sum;
            return;
        }
        board[i][j] = 'B';
        for (int x = i - 1; x <= i + 1; x++)
        {
            for (int y = j - 1; y <= j + 1; y++)
            {
                if (x >= 0 && x < m && y >= 0 && y < n && board[x][y] == 'E')
                    dfs(board, x, y);
            }
        }
    }
};